<template>
  <div class="login-page">
    <!-- 顶部栏 -->
    <van-nav-bar title="面经登录" ></van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field   v-model="username" label="用户名"  name="username"  placeholder="用户名"
      :rules="[
      { required: true, message: '请填写用户名' },
      {pattern:/^\w{5,15}$/, message: '用户名是5-15位'}]" />
      <van-field    v-model="password" label="密码"  name="password" placeholder="密码"
       :rules="[
       { required: true, message: '请填写密码' },
       {pattern:/^\w{5,15}$/,message: '密码是5-15位'}]" />
      <div style="margin: 16px;"></div>
      <van-button round block type="info" native-type="submit">提交</van-button>
    </van-form>
    <!-- 跳转注册页 -->
    <a  class="link" href="#/register">跳转注册页</a>
  </div>
</template>

<script>
/*  导入user组件 */
import { userLogin } from '@/api/user.js'
import { setToken } from '@/utils/storage.js'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // (1)获取表单数据 需要设置name属性
      console.log(values)// 表单数据
      // (2)非空+正则校验:组件自带配置
      // (3)发送ajax
      const data = await userLogin(values)
      console.log(data)
      setToken(data.data.token)
      // 请求成功 跳转首页
      this.$router.push('/layout')
    }
  }
}
</script>

<style lang="less" scoped>
.login-page{
  padding: 15px;
  .link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  padding-top: 8px;
  float: right;
}
}

</style>
